/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FangYongliang
 * 创建时间:    2023-10-13
 */
package org.jeecg.modules.dock.handler;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.basic.warehouseline.service.IWmsMesLineWarehouseMapService;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.dock.constant.SystemConstants;
import org.jeecg.modules.dock.controller.EmsDockingController;
import org.jeecg.modules.dock.entity.DockDataReceiveRecord;
import org.jeecg.modules.dock.entity.DockTaskRecordMap;
import org.jeecg.modules.dock.entity.mds.request.MdsBaseRequestVO;
import org.jeecg.modules.dock.entity.mes.request.MesBaseRequestVO;
import org.jeecg.modules.dock.enums.DockDataStatus;
import org.jeecg.modules.dock.enums.EmsBucketStatusEnum;
import org.jeecg.modules.dock.enums.MdsInterfaceTypeEnum;
import org.jeecg.modules.dock.enums.MdsStatusEnum;
import org.jeecg.modules.dock.mapper.DockTaskRecordMapMapper;
import org.jeecg.modules.dock.service.MesDockingRepeaterService;
import org.jeecg.modules.flow.record.entity.WmsFlowRecord;
import org.jeecg.modules.flow.record.service.IWmsFlowRecordService;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.jeecg.modules.flow.sys.util.SpringContextUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * //TODO 【请填写类描述】
 *
 * @author FangYongliang
 * @version 1.0.0
 * @since 2023-10-13
 */
@Slf4j
public class MdsStatusHandler implements StatusHandler<MdsBaseRequestVO> {


    private DockTaskRecordMapMapper dockTaskRecordMapMapper;
    private IWmsFlowRecordService iWmsFlowRecordService;

    private ArrayList<String> abnormalStatus = Lists.newArrayList(EmsBucketStatusEnum.CANCEL_TASK.getCode(),
            EmsBucketStatusEnum.PAUSE_TASK.getCode(), EmsBucketStatusEnum.FORCE_FINISHED.getCode());

    public MdsStatusHandler() {
        this.dockTaskRecordMapMapper = SpringContextUtil.getBean(DockTaskRecordMapMapper.class);
        this.iWmsFlowRecordService = SpringContextUtil.getBean(IWmsFlowRecordService.class);
    }

    private DockDataReceiveRecord getRecord(String data) {
        DockDataReceiveRecord dockDataReceiveRecord = new DockDataReceiveRecord();
        dockDataReceiveRecord.setUuid(IdUtil.fastSimpleUUID());
        dockDataReceiveRecord.setSource(SystemConstants.MDS);
        dockDataReceiveRecord.setTarget(SystemConstants.WMS);
        dockDataReceiveRecord.setData(data);
        dockDataReceiveRecord.setReceiveTime(new Date());
        dockDataReceiveRecord.setStatus(DockDataStatus.SUCCESS);
        return dockDataReceiveRecord;
    }

    String getNodeName(String flowCode, String taskCode) {
        DockTaskRecordMap dockTaskRecordMap = dockTaskRecordMapMapper.getByTaskCode(taskCode);
        if (ObjectUtil.isNotNull(dockTaskRecordMap)) {
            WmsFlowRecord record = iWmsFlowRecordService.getOne(new LambdaQueryWrapper<WmsFlowRecord>()
                    .eq(WmsFlowRecord::getFlowId, dockTaskRecordMap.getTaskId())
                    .eq(WmsFlowRecord::getFlowCode, flowCode)
                    .last("limit 1")
            );
            if (ObjectUtil.isNotNull(record)) {
                return record.getFlowName();
            }
        }
        return null;
    }

    @Override
    public void handle(MdsBaseRequestVO vo) {
        DockDataReceiveRecord record = getRecord(vo.toString());
        record.setBusinessCode(vo.getTaskType());
        HttpServletRequest httpServletRequest = SpringContextUtils.getHttpServletRequest();
        httpServletRequest.setAttribute(SystemConstants.AttachKey.RECEIVE, record);
        MesDockingRepeaterService mesDockingRepeaterService = SpringContextUtil.getBean(MesDockingRepeaterService.class);
        IWmsOutReceiptTaskService outReceiptTaskService = SpringContextUtil.getBean(IWmsOutReceiptTaskService.class);
        try {
            DockTaskRecordMap map = dockTaskRecordMapMapper.selectOne(new LambdaQueryWrapper<DockTaskRecordMap>()
                    .eq(DockTaskRecordMap::getMesTaskCode, vo.getTaskCode()));

            //横剪叫料
            if (MdsInterfaceTypeEnum.HJ_TAKE.getCode().equals(vo.getTaskType())) {

                if (MdsStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.pauseFlow(map.getTaskId());
                } else if (MdsStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.cancelFlow(map.getTaskId());
                } else if (MdsStatusEnum.TASK_EXCEPTION.getCode().equals(vo.getTaskStatus())) {
                    this.errorFlow(map.getTaskId(), vo.getRemark());
                } else if (MdsStatusEnum.SEND_PALLET_DONE.getCode().equals(vo.getTaskStatus())) {
                    // 600线节点
                    if ("ADZ-04".equals(vo.getSite())) {
                        final String CODE = "8aaa85df8ca38f68018ca39846e80002";
                        record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                        commitMds(vo, CODE);
                    }else {
                        final String CODE = "8aaa81b78b36e0be018b36e4a4b90001";
                        record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                        commit(vo, CODE);
                    }
                    printLog(MdsStatusEnum.SEND_PALLET_DONE);
                } else if (MdsStatusEnum.SEND_MATERIAL_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    // 600线节点
                    if ("ADZ-04".equals(vo.getSite())) {
                        final String CODE = "8aaa85df8ca38f68018ca39a3bb40005";
                        record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                        commitMds(vo, CODE);
                    }else {
                        final String CODE = "8aaa81a98b422f9f018b423034b60001"; //8aaa81a98b422f9f018b423034b60001
                        record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                        commit(vo, CODE);
                    }

                    printLog(MdsStatusEnum.SEND_MATERIAL_FINISHED);
                } else if (MdsStatusEnum.TASK_STARTED.getCode().equals(vo.getTaskStatus())) {
                    //更新备料站点
                    if (StringUtils.isBlank(vo.getSite())){
                        log.error(MdsInterfaceTypeEnum.HJ_TAKE.getRemark()+" "+vo.getTaskStatus() + "站点site字段为空");
                    }
                    WmsOutReceiptTask outReceiptTask = new WmsOutReceiptTask();
                    outReceiptTask.setId(map.getTaskId());
                    outReceiptTask.setMaterialSite(vo.getSite());
                    outReceiptTaskService.updateById(outReceiptTask);
                    printLog(MdsStatusEnum.TASK_STARTED);
                } else if (MdsStatusEnum.TAKE_PALLET_DONE.getCode().equals(vo.getTaskStatus())) {

                    printLog(MdsStatusEnum.TAKE_PALLET_DONE);
                } else if (MdsStatusEnum.TAKE_MATERIAL_STARTED.getCode().equals(vo.getTaskStatus())) {

                    printLog(MdsStatusEnum.TAKE_MATERIAL_STARTED);
                } else if (MdsStatusEnum.TAKE_MATERIAL_FINISHED.getCode().equals(vo.getTaskStatus())) {

                    printLog(MdsStatusEnum.TAKE_MATERIAL_FINISHED);
                }

                MesBaseRequestVO mesBaseRequestVO = BeanUtil.toBean(vo, MesBaseRequestVO.class);
                mesDockingRepeaterService.updateStatus(mesBaseRequestVO);
            }
            //横剪退料
            else if (MdsInterfaceTypeEnum.HJ_RETURN.getCode().equals(vo.getTaskType())) {
                if (MdsStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.pauseFlow(map.getTaskId());
                } else if (MdsStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.cancelFlow(map.getTaskId());
                } else if (MdsStatusEnum.TASK_EXCEPTION.getCode().equals(vo.getTaskStatus())) {
                    this.errorFlow(map.getTaskId(), vo.getRemark());
                } else if (MdsStatusEnum.FORCE_FINISHED.getCode().equals(vo.getTaskStatus())) {

                } else if (MdsStatusEnum.SEND_MATERIAL_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    final String CODE = "8aaa81768b178712018b178712290001";
                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                    printLog(MdsStatusEnum.SEND_MATERIAL_FINISHED);
                    commit(vo, CODE);


                } else if (MdsStatusEnum.RETURN_PALLET_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    final String CODE = "8aaa81b98b36fc03018b36fc31cd0001";
                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                    printLog(MdsStatusEnum.RETURN_PALLET_FINISHED);
                    commit(vo, CODE);

                } else if (MdsStatusEnum.TASK_STARTED.getCode().equals(vo.getTaskStatus())) {
                    printLog(MdsStatusEnum.TASK_STARTED);
                    //更新退料站点
                    if (StringUtils.isBlank(vo.getSite())){
                        log.error(MdsInterfaceTypeEnum.HJ_RETURN.getRemark()+" "+vo.getTaskStatus() + "站点site字段为空");
                    }
                    IWmsInReceiptTaskService taskService = SpringContextUtil.getBean(IWmsInReceiptTaskService.class);
                    WmsInReceiptTask task = new WmsInReceiptTask();
                    task.setId(map.getTaskId());
                    task.setMaterialSite(vo.getSite());
                    taskService.updateById(task);

                } else if (MdsStatusEnum.TAKE_MATERIAL_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    printLog(MdsStatusEnum.TAKE_MATERIAL_FINISHED);

                } else if (MdsStatusEnum.TAKE_PALLET_STARTED.getCode().equals(vo.getTaskStatus())) {
                    printLog(MdsStatusEnum.TAKE_PALLET_STARTED);

                } else if (MdsStatusEnum.TAKE_PALLET_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    printLog(MdsStatusEnum.TAKE_PALLET_FINISHED);
                }
                MesBaseRequestVO mesBaseRequestVO = BeanUtil.toBean(vo, MesBaseRequestVO.class);
                mesDockingRepeaterService.updateStatus(mesBaseRequestVO);
            }
            //横剪叫料板
            else if (MdsInterfaceTypeEnum.HJ_TAKE_PLANK.getCode().equals(vo.getTaskType())) {
                if (MdsStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.pauseFlow(map.getTaskId());
                } else if (MdsStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.cancelFlow(map.getTaskId());
                } else if (MdsStatusEnum.TASK_EXCEPTION.getCode().equals(vo.getTaskStatus())) {
                    this.errorFlow(map.getTaskId(), vo.getRemark());
                } else if (MdsStatusEnum.FORCE_FINISHED.getCode().equals(vo.getTaskStatus())) {

                } else if (MdsStatusEnum.SEND_PLANK_DONE.getCode().equals(vo.getTaskStatus())) {
                    final String CODE = "8aaa81be8b3715a6018b371643eb0001";
                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                    printLog(MdsStatusEnum.SEND_PLANK_DONE);
                    commit(vo, CODE);
                } else if (MdsStatusEnum.TASK_STARTED.getCode().equals(vo.getTaskStatus())) {
                    printLog(MdsStatusEnum.TASK_STARTED);
                } else if (MdsStatusEnum.TAKE_PLANK_DONE.getCode().equals(vo.getTaskStatus())) {
                    printLog(MdsStatusEnum.TAKE_PLANK_DONE);
                }
                MesBaseRequestVO mesBaseRequestVO = BeanUtil.toBean(vo, MesBaseRequestVO.class);
                mesDockingRepeaterService.updateStatus(mesBaseRequestVO);
            }
            //横剪叫废料箱
            else if (MdsInterfaceTypeEnum.HJ_TAKE_WASTE.getCode().equals(vo.getTaskType())) {
                boolean isSend = false;
                String status = vo.getTaskStatus();
                if (MdsStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.pauseFlow(map.getTaskId());
                } else if (MdsStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.cancelFlow(map.getTaskId());
                } else if (MdsStatusEnum.TASK_EXCEPTION.getCode().equals(vo.getTaskStatus())) {
                    this.errorFlow(map.getTaskId(), vo.getRemark());
                } else if (MdsStatusEnum.FORCE_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    isSend = true;
                    status = MdsStatusEnum.FORCE_FINISHED.getCode();
                } else if (MdsStatusEnum.SEND_WASTE_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    isSend = true;
                    status = MdsStatusEnum.SEND_WASTE_FINISHED.getCode();
                }else if (MdsStatusEnum.TASK_STARTED.getCode().equals(vo.getTaskStatus())){
                    isSend = true;
                }
                MesBaseRequestVO mesBaseRequestVO = BeanUtil.toBean(vo, MesBaseRequestVO.class);
                mesDockingRepeaterService.updateStatus(mesBaseRequestVO);
            }
            //横剪退废料箱
            else if (MdsInterfaceTypeEnum.HJ_RETURN_WASTE.getCode().equals(vo.getTaskType())) {
                boolean isSend = false;
                String status = "";
                if (MdsStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.pauseFlow(map.getTaskId());
                } else if (MdsStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.cancelFlow(map.getTaskId());
                } else if (MdsStatusEnum.TASK_EXCEPTION.getCode().equals(vo.getTaskStatus())) {
                    this.errorFlow(map.getTaskId(), vo.getRemark());
                } else if (MdsStatusEnum.FORCE_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    isSend = true;
                    status = MdsStatusEnum.FORCE_FINISHED.getCode();
                } else if (MdsStatusEnum.RETURN_WASTE_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    isSend = true;
                    status = MdsStatusEnum.RETURN_WASTE_FINISHED.getCode();
                }

                MesBaseRequestVO mesBaseRequestVO = BeanUtil.toBean(vo, MesBaseRequestVO.class);
                mesDockingRepeaterService.updateStatus(mesBaseRequestVO);
            }
            //横剪入库
            else if (MdsInterfaceTypeEnum.HJ_STOCK.getCode().equals(vo.getTaskType())) {
                if (MdsStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.pauseFlow(map.getTaskId());
                } else if (MdsStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.cancelFlow(map.getTaskId());
                } else if (MdsStatusEnum.TASK_EXCEPTION.getCode().equals(vo.getTaskStatus())) {
                    this.errorFlow(map.getTaskId(), vo.getRemark());
                } else if (MdsStatusEnum.FORCE_FINISHED.getCode().equals(vo.getTaskStatus())) {

                } else if (MdsStatusEnum.STORE_MATERIAL_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    final String CODE = "8aaa820f8b133d87018b1341acc50010";
                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                    printLog(MdsStatusEnum.STORE_MATERIAL_FINISHED);
                    commit(vo, CODE);
                } else if (MdsStatusEnum.TASK_STARTED.getCode().equals(vo.getTaskStatus())) {
                    printLog(MdsStatusEnum.TASK_STARTED);
                } else if (MdsStatusEnum.TAKE_MATERIAL_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    printLog(MdsStatusEnum.TAKE_MATERIAL_FINISHED);
                }
                MesBaseRequestVO mesBaseRequestVO = BeanUtil.toBean(vo, MesBaseRequestVO.class);
                mesDockingRepeaterService.updateStatus(mesBaseRequestVO);
            }
            //叠片叫料
            else if (MdsInterfaceTypeEnum.DP_TAKE.getCode().equals(vo.getTaskType())) {
                if (MdsStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.pauseFlow(map.getTaskId());
                } else if (MdsStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.cancelFlow(map.getTaskId());
                } else if (MdsStatusEnum.TASK_EXCEPTION.getCode().equals(vo.getTaskStatus())) {
                    this.errorFlow(map.getTaskId(), vo.getRemark());
                } else if (MdsStatusEnum.FORCE_FINISHED.getCode().equals(vo.getTaskStatus())) {

                } else if (MdsStatusEnum.SEND_MATERIAL_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    final String CODE = "8aaa81c38b372af6018b372b3af00001";
                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                    printLog(MdsStatusEnum.SEND_MATERIAL_FINISHED);
                    commit(vo, CODE);
                } else if (MdsStatusEnum.TASK_STARTED.getCode().equals(vo.getTaskStatus())) {
                    printLog(MdsStatusEnum.TASK_STARTED);
                } else if (MdsStatusEnum.TAKE_MATERIAL_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    printLog(MdsStatusEnum.TAKE_MATERIAL_FINISHED);
                }
                MesBaseRequestVO mesBaseRequestVO = BeanUtil.toBean(vo, MesBaseRequestVO.class);
                mesDockingRepeaterService.updateStatus(mesBaseRequestVO);
            }
            //叠片退料
            else if (MdsInterfaceTypeEnum.DP_RETURN.getCode().equals(vo.getTaskType())) {
                if (MdsStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.pauseFlow(map.getTaskId());
                } else if (MdsStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.cancelFlow(map.getTaskId());
                } else if (MdsStatusEnum.TASK_EXCEPTION.getCode().equals(vo.getTaskStatus())) {
                    this.errorFlow(map.getTaskId(), vo.getRemark());
                } else if (MdsStatusEnum.FORCE_FINISHED.getCode().equals(vo.getTaskStatus())) {

                } else if (MdsStatusEnum.RETURN_MATERIAL_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    final String CODE = "8aaa81568b5f9f8a018b5f9f8acc0000";
                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                    printLog(MdsStatusEnum.RETURN_MATERIAL_FINISHED);
                    commit(vo, CODE);
                } else if (MdsStatusEnum.TASK_STARTED.getCode().equals(vo.getTaskStatus())) {
                    printLog(MdsStatusEnum.TASK_STARTED);
                } else if (MdsStatusEnum.TAKE_MATERIAL_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    printLog(MdsStatusEnum.TAKE_MATERIAL_FINISHED);
                }
                MesBaseRequestVO mesBaseRequestVO = BeanUtil.toBean(vo, MesBaseRequestVO.class);
                mesDockingRepeaterService.updateStatus(mesBaseRequestVO);
            }
            //叠片退料板
            else if (MdsInterfaceTypeEnum.DP_RETURN_PLANK.getCode().equals(vo.getTaskType())) {
                if (MdsStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.pauseFlow(map.getTaskId());
                } else if (MdsStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.cancelFlow(map.getTaskId());
                } else if (MdsStatusEnum.TASK_EXCEPTION.getCode().equals(vo.getTaskStatus())) {
                    this.errorFlow(map.getTaskId(), vo.getRemark());
                } else if (MdsStatusEnum.FORCE_FINISHED.getCode().equals(vo.getTaskStatus())) {

                } else if (MdsStatusEnum.RETURN_PLANK_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    final String CODE = "8aaa81ec8b3b1a61018b3b1a61820000";
                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                    printLog(MdsStatusEnum.RETURN_PLANK_FINISHED);
                    commit(vo, CODE);
                } else if (MdsStatusEnum.TASK_STARTED.getCode().equals(vo.getTaskStatus())) {
                    printLog(MdsStatusEnum.TASK_STARTED);
                } else if (MdsStatusEnum.TAKE_PLANK_DONE.getCode().equals(vo.getTaskStatus())) {
                    printLog(MdsStatusEnum.TAKE_PLANK_DONE);
                }
                MesBaseRequestVO mesBaseRequestVO = BeanUtil.toBean(vo, MesBaseRequestVO.class);
                mesDockingRepeaterService.updateStatus(mesBaseRequestVO);
            }
            //条料调拨入库
            else if (MdsInterfaceTypeEnum.TL_ALLOT_STOCK.getCode().equals(vo.getTaskType())) {
                if (MdsStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.pauseFlow(map.getTaskId());
                } else if (MdsStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.cancelFlow(map.getTaskId());
                } else if (MdsStatusEnum.TASK_EXCEPTION.getCode().equals(vo.getTaskStatus())) {
                    this.errorFlow(map.getTaskId(), vo.getRemark());
                } else if (MdsStatusEnum.FORCE_FINISHED.getCode().equals(vo.getTaskStatus())) {

                }else if (MdsStatusEnum.SEND_MATERIAL_FINISHED.getCode().equals(vo.getTaskStatus())){
                    final String CODE = "8aaa84ab8c0f805a018c0f805a2f0000";
                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                    commit(vo, CODE);
                    printLog(MdsStatusEnum.SEND_MATERIAL_FINISHED);
                }else if (MdsStatusEnum.RETURN_PALLET_FINISHED.getCode().equals(vo.getTaskStatus())){
                    printLog(MdsStatusEnum.RETURN_PALLET_FINISHED);
                    final String CODE = "8aaa84ab8c0f805a018c0f8142650003";
                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                    commit(vo, CODE);

                }else if (MdsStatusEnum.TASK_STARTED.getCode().equals(vo.getTaskStatus())){
                    printLog(MdsStatusEnum.TASK_STARTED);
                    //更新退料站点
                    if (StringUtils.isBlank(vo.getSite())){
                        log.error(MdsInterfaceTypeEnum.TL_ALLOT_STOCK.getRemark()+" "+vo.getTaskStatus() + "站点site字段为空");
                    }
                    IWmsMoveReceiptService taskService = SpringContextUtil.getBean(IWmsMoveReceiptService.class);
                    WmsMoveReceipt task = new WmsMoveReceipt();
                    task.setId(map.getTaskId());
                    task.setMaterialSite(vo.getSite());
                    taskService.updateById(task);
                }
//                MesBaseRequestVO mesBaseRequestVO = BeanUtil.toBean(vo, MesBaseRequestVO.class);
//                mesDockingRepeaterService.updateStatus(mesBaseRequestVO);
            }
            //条料库叫料架
            else if (MdsInterfaceTypeEnum.TL_ALLOT_PALLET.getCode().equals(vo.getTaskType())) {
                if (MdsStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.pauseFlow(map.getTaskId());
                } else if (MdsStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.cancelFlow(map.getTaskId());
                } else if (MdsStatusEnum.TASK_EXCEPTION.getCode().equals(vo.getTaskStatus())) {
                    this.errorFlow(map.getTaskId(), vo.getRemark());
                } else if (MdsStatusEnum.FORCE_FINISHED.getCode().equals(vo.getTaskStatus())) {

                }else if (MdsStatusEnum.SEND_PALLET_DONE.getCode().equals(vo.getTaskStatus())){
                    final String CODE = "8aaa82e28cc424b1018cc4253dc60001";
                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                    commit(vo, CODE);
                    printLog(MdsStatusEnum.SEND_PALLET_DONE);
                }
                MesBaseRequestVO mesBaseRequestVO = BeanUtil.toBean(vo, MesBaseRequestVO.class);
                mesDockingRepeaterService.updateStatus(mesBaseRequestVO);
            }
            //条料调拨出库
            else if (MdsInterfaceTypeEnum.TL_ALLOT_OUTBOUND.getCode().equals(vo.getTaskType())) {
                if (MdsStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.pauseFlow(map.getTaskId());
                } else if (MdsStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.cancelFlow(map.getTaskId());
                } else if (MdsStatusEnum.TASK_EXCEPTION.getCode().equals(vo.getTaskStatus())) {
                    this.errorFlow(map.getTaskId(), vo.getRemark());
                } else if (MdsStatusEnum.FORCE_FINISHED.getCode().equals(vo.getTaskStatus())) {

                }
//                else if (MdsStatusEnum.SEND_PALLET_DONE.getCode().equals(vo.getTaskStatus())){
//                    final String CODE = "8aaa82e28cc424b1018cc4253dc60001";
//                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
//                    commit(vo, CODE);
//                    printLog(MdsStatusEnum.SEND_PALLET_DONE);
//                }
                else if (MdsStatusEnum.SEND_MATERIAL_FINISHED.getCode().equals(vo.getTaskStatus())){
                    final String CODE = "8aaa82e28cc424b1018cc4269ec10004";
                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                    commit(vo, CODE);
                    printLog(MdsStatusEnum.SEND_MATERIAL_FINISHED);
                }else if (MdsStatusEnum.TASK_STARTED.getCode().equals(vo.getTaskStatus())){
                    printLog(MdsStatusEnum.TASK_STARTED);
                    //更新备料站点
                    if (StringUtils.isBlank(vo.getSite())){
                        log.error(MdsInterfaceTypeEnum.TL_ALLOT_OUTBOUND.getRemark()+" "+vo.getTaskStatus() + "站点site字段为空");
                    }
                    IWmsMoveReceiptService taskService = SpringContextUtil.getBean(IWmsMoveReceiptService.class);
                    WmsMoveReceipt task = new WmsMoveReceipt();
                    task.setId(map.getTaskId());
                    task.setMaterialSite(vo.getSite());
                    taskService.updateById(task);
                }
                MesBaseRequestVO mesBaseRequestVO = BeanUtil.toBean(vo, MesBaseRequestVO.class);
                mesDockingRepeaterService.updateStatus(mesBaseRequestVO);
            } else if (MdsInterfaceTypeEnum.ZJ_RETURN_PAPER.getCode().equals(vo.getTaskType())) {
                if (MdsStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.pauseFlow(map.getTaskId());
                } else if (MdsStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.cancelFlow(map.getTaskId());
                } else if (MdsStatusEnum.TASK_EXCEPTION.getCode().equals(vo.getTaskStatus())) {
                    this.errorFlow(map.getTaskId(), vo.getRemark());
                } else if (MdsStatusEnum.FORCE_FINISHED.getCode().equals(vo.getTaskStatus())) {

                } else if (MdsStatusEnum.TAKE_BUCKET_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    final String CODE = "8aaa81738b1769fb018b176d1abb0005";
                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                    printLog(MdsStatusEnum.TAKE_BUCKET_FINISHED);
                    commit(vo, CODE);
                } else if (MdsStatusEnum.RETURN_BUCKET_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    final String CODE = "8aaa81738b1769fb018b17711c3e0007";
                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                    printLog(MdsStatusEnum.RETURN_BUCKET_FINISHED);
                    commit(vo, CODE);
                }
                MesBaseRequestVO mesBaseRequestVO = BeanUtil.toBean(vo, MesBaseRequestVO.class);
                mesDockingRepeaterService.updateStatus(mesBaseRequestVO);
            } else if (MdsInterfaceTypeEnum.ZJ_TAKE_PAPER.getCode().equals(vo.getTaskType())) {
                if (MdsStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.pauseFlow(map.getTaskId());
                } else if (MdsStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                    this.cancelFlow(map.getTaskId());
                } else if (MdsStatusEnum.TASK_EXCEPTION.getCode().equals(vo.getTaskStatus())) {
                    this.errorFlow(map.getTaskId(), vo.getRemark());
                } else if (MdsStatusEnum.FORCE_FINISHED.getCode().equals(vo.getTaskStatus())) {

                } else if (MdsStatusEnum.SEND_BUCKET_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    final String CODE = "8aaa81af8b365b8e018b365c73c60003";
                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                    printLog(MdsStatusEnum.SEND_BUCKET_FINISHED);
                    commit(vo, CODE);
                } else if (MdsStatusEnum.TAKE_BUCKET_FINISHED.getCode().equals(vo.getTaskStatus())) {
                    final String CODE = "8aaa81af8b365b8e018b365b8ea30000";
                    record.setNodeName(getNodeName(CODE, vo.getTaskCode()));
                    printLog(MdsStatusEnum.TAKE_BUCKET_FINISHED);
                    commit(vo, CODE);
                }
                MesBaseRequestVO mesBaseRequestVO = BeanUtil.toBean(vo, MesBaseRequestVO.class);
                mesDockingRepeaterService.updateStatus(mesBaseRequestVO);
            }
        } catch (Exception e) {
            log.error("MDS推送状态发生异常", e);
            record.setStatus(DockDataStatus.FAILURE);
            record.setResultData(e.toString());
        }

    }

    private void printLog(MdsStatusEnum mdsStatusEnum) {
        log.info("开始" + mdsStatusEnum.getRemark() + "节点扭转");
    }

    private void commit(MdsBaseRequestVO vo, String code) {
        DockTaskRecordMap map = dockTaskRecordMapMapper.selectOne(new LambdaQueryWrapper<DockTaskRecordMap>()
                .eq(DockTaskRecordMap::getMesTaskCode, vo.getTaskCode()));
        if (ObjectUtil.isEmpty(map)) {
            throw new JeecgBootException("未通过taskCode查询到任务id");
        }
        String taskId = map.getTaskId();
        BaseFlowDto baseFlowDto = new BaseFlowDto();
        baseFlowDto.setInfoId(taskId);
        baseFlowDto.setCode(code);
        workFlowService.commit(baseFlowDto);
    }

    /**
     * 区分横剪叫料，区分mds手动或者自动,来源于mds是自动
     *
     * @param vo
     * @param code
     */
    private void commitMds(MdsBaseRequestVO vo, String code) {
        DockTaskRecordMap map = dockTaskRecordMapMapper.selectOne(new LambdaQueryWrapper<DockTaskRecordMap>()
                .eq(DockTaskRecordMap::getMesTaskCode, vo.getTaskCode()));
        if (ObjectUtil.isEmpty(map)) {
            throw new JeecgBootException("未通过taskCode查询到任务id");
        }
        String taskId = map.getTaskId();
        BaseFlowDto baseFlowDto = new BaseFlowDto();
        baseFlowDto.setInfoId(taskId);
        baseFlowDto.setCode(code);
        Map<String, Boolean> dataMap = new HashMap<>();
        dataMap.put("fromMDSFlag", true);
        baseFlowDto.setData(dataMap);
        workFlowService.commit(baseFlowDto);
    }

    private MesBaseRequestVO getRequest(EmsDockingController.TaskTypeUpdateParam param) {
        return getRequest(param, param.getTaskStatus());
    }

    private MesBaseRequestVO getRequest(EmsDockingController.TaskTypeUpdateParam param, String taskStatus) {
        MesBaseRequestVO requestVO = new MesBaseRequestVO();
        requestVO.setTaskCode(param.getTaskCode());
        requestVO.setTaskStatus(taskStatus);
        requestVO.setSite(param.getSite());
        requestVO.setOperateTime(new Date());
        requestVO.setTaskType(param.getTaskType());
        requestVO.setLineName(param.getLineName());
        requestVO.setOperateReason(param.getOperateReason());
        requestVO.setRemark(param.getRemark());
        requestVO.setOperatorName(param.getOperatorName());
        return requestVO;
    }

    /**
     * 取消配送，关闭流程
     */
    private void errorFlow(String infoId, String msg) {
        BaseFlowDto base = new BaseFlowDto();
        base.setInfoId(infoId);
        base.setRefuseType(4);
        base.setRemark(msg);
        workFlowService.interrupt(base);
    }


}
